package com.haohan.stock.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Service;

import com.haohan.stock.domain.StockData;
import com.haohan.util.CodeList;
import com.haohan.util.DataUtil;

/**
 * 奖项数据操作类
 * 
 * @author Administrator
 *
 */
@Service
public class StockLoneService {

	public void saveInitOne(int scale) throws IOException {
		String[] a = CodeList.codes;
		for (int i = 0; i < a.length; i++) {
			System.out.println("BEGIN===" + (i + 1) + "-------------开始保存" + a[i]);
			String str = saveOne(a[i], scale);
			FileUtils.write(new File("F:\\"+DateUtils.formatDate(new Date(), "yyyy-MM-dd")+".json"), str.replaceFirst("]", ""), "UTF-8", true);
			System.out.println("END===" + (i + 1) + "-------------结束保存" + a[i]);
		}
		FileUtils.write(new File("F:\\"+DateUtils.formatDate(new Date(), "yyyy-MM-dd")+".json"), "]", "UTF-8", true);
	}

	private String saveOne(String code, int scale) {
		List<StockData> list = DataUtil.getStockData(code, scale, 31);
		if (list == null || list.isEmpty()) {
			return "";
		}
		calcPrice(list, 5);
		calcPrice(list, 10);
		calcPrice(list, 20);
		calcPrice(list, 30);
		calcVolume(list, 5);
		calcVolume(list, 10);
		calcVolume(list, 20);
		calcVolume(list, 30);
		calcPoint(list);
		setid(list, code, scale);
		List<StockData> listResult = new ArrayList<StockData>();
		listResult.add(list.get(list.size()-1));
		return DataUtil.objectToJson(listResult);
	}

	private void setid(List<StockData> list, String code, int scale) {
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData stockData = (StockData) iterator.next();
			stockData.setCode(code);
			stockData.setScale(scale);
		}
	}

	private void calcPoint(List<StockData> list) {
		Integer i = 0;
		Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData stockData = (StockData) iterator.next();
			i++;
			boolean checkModern = false;
			if (stockData.getClose() > stockData.getPrice5() && stockData.getClose() > stockData.getPrice10()
					&& stockData.getClose() > stockData.getPrice20() && stockData.getClose() > stockData.getPrice30()) {
				checkModern = true;
			} else {
				checkModern = false;
			}
			if (stockData.getClose() < stockData.getPrice5() && stockData.getClose() < stockData.getPrice10()
					&& stockData.getClose() < stockData.getPrice20() && stockData.getClose() < stockData.getPrice30()) {
				map.put(i, true);
			} else {
				map.put(i, false);
			}
			if (map.get(i - 1) != null && map.get(i - 1) && checkModern) {
				stockData.setBuy_point(true);
			}
		}
	}

	private void calcPrice(List<StockData> list, int param) {
		Map<String, Double> map = new HashMap<String, Double>();
		for (int i = 0; i < list.size(); i++) {
			double pricema = 0d;
			if ((i + param - 1) >= list.size()) {
				break;
			}
			for (int j = i; j < i + param; j++) {
				pricema += list.get(j).getClose();
			}
			map.put(list.get(i + param - 1).getDay(), pricema / (param + 0d));
		}
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData StockThirtyMinutes = (StockData) iterator.next();
			if (map.get(StockThirtyMinutes.getDay()) == null) {
				continue;
			}
			if (param == 5) {
				StockThirtyMinutes.setPrice5(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 10) {
				StockThirtyMinutes.setPrice10(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 20) {
				StockThirtyMinutes.setPrice20(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 30) {
				StockThirtyMinutes.setPrice30(map.get(StockThirtyMinutes.getDay()));
			}
		}
	}

	private void calcVolume(List<StockData> list, int param) {
		Map<String, Double> map = new HashMap<String, Double>();
		for (int i = 0; i < list.size(); i++) {
			double pricema = 0d;
			if ((i + param - 1) >= list.size()) {
				break;
			}
			for (int j = i; j < i + param; j++) {
				pricema += list.get(j).getVolume();
			}
			map.put(list.get(i + param - 1).getDay(), pricema / (param + 0d));
		}
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData StockThirtyMinutes = (StockData) iterator.next();
			if (map.get(StockThirtyMinutes.getDay()) == null) {
				continue;
			}
			if (param == 5) {
				StockThirtyMinutes.setVolume5(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 10) {
				StockThirtyMinutes.setVolume10(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 20) {
				StockThirtyMinutes.setVolume20(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 30) {
				StockThirtyMinutes.setVolume30(map.get(StockThirtyMinutes.getDay()));
			}
		}
	}

}
